/*
 *  Copyright (c) 2023-2025 苏州棱镜七彩信息科技有限公司 (Suzhou PrismAurora Information Technology Co.,Ltd)
 *  sbom-generate is licensed under Mulan PSL v2.
 *  You can use this software according to the terms and conditions of the Mulan PSL v2.
 *  You may obtain a copy of Mulan PSL v2 at:
 *           http://license.coscl.org.cn/MulanPSL2
 *  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
 * */
package com.ljqc.entity;

import com.ljqc.utils.CommandUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Command
 * @Description
 * @Date 2022/8/11 11:05
 * @Version V1.0
 **/
public class Command {
    protected final StringBuilder sb = new StringBuilder();

    public static Command create() {
        return new Command();
    }

    protected static final Map<String, String> ENVIRONMENT_VARIABLE_MAP = new HashMap<>();

    public static void initEnvironmentVariable(Map<String, String> params) {
        Command.ENVIRONMENT_VARIABLE_MAP.putAll(params);
    }

    public Command cd(String path) {
        if (CommandUtils.isWindows) {
            appendPrefix();
            String chkdsk = path.substring(0, path.indexOf(":") + 1);
            sb.append(chkdsk).append(" ");
        }
        appendPrefix();
        sb.append("cd").append(" \"").append(path).append("\"");
        return this;
    }

    public Command command(String command, Object... parameter) {
        appendPrefix();
        sb.append(String.format(command, parameter));
        return this;
    }

    protected void appendPrefix() {
        if (sb.length() > 0) {
            sb.append(" ").append("&&").append(" ");
        }
    }

    public String build() {
        return sb.toString();
    }
}
